package net.kehuilai.handler;

import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import net.kehuilai.model.BaseMessage;

import java.util.List;


/**
 * Created by genghz on 16/12/6.
 */
public class MessageDecoderHandler extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext cxt, ByteBuf byteBuf, List<Object> list) throws Exception {
        byte[] bytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(bytes);       // byteBuf --> byte
        String msg = new String(bytes);
        BaseMessage baseMessage = JSON.parseObject(msg, BaseMessage.class);
        list.add(baseMessage);
    }
}
